package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCommentServiceImpl implements AdminCommentService{
    @Override
    public List<MarketComment> list(String page, String limit, String sort, String order, String userId, String valueId) {
        SqlSession session = MybatisUtils.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        if (!StringUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(valueId)){
            criteria.andValueIdEqualTo(Integer.valueOf(valueId));
        }
        marketCommentExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
        List<MarketComment> marketComments = mapper.selectByExample(marketCommentExample);
        session.close();

        return marketComments;
    }

    @Override
    public int delete(Integer id) {
        SqlSession session = MybatisUtils.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        int i = mapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        return i;
    }
}
